package org.example.smart.server;

import com.briup.smart.env.entity.Environment;
import com.briup.smart.env.server.Server;

import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;

public class ServerImpl implements Server {

    @Override
    public void receive(){
        try ( ServerSocket serverSocket = new ServerSocket(8030);
              Socket socket = serverSocket.accept();
              ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());){
            System.out.println("成功启动客户端");
            Collection<Environment> coll = new ArrayList<>();
            coll = (Collection<Environment>) ois.readObject();
            System.out.println("接收数据成功共" +coll.size() + "条！");
//            for (Environment environment : coll) {
//                System.out.println(environment);
//            }
            DBStoreImpl dbStore = new DBStoreImpl();
            dbStore.saveDB(coll);;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void shutdown() throws Exception {

    }
}
